package com.xbongbong.paas.enums;

import com.xbongbong.i18n.util.I18nMessageUtil;
import lombok.Getter;
import lombok.ToString;

import java.util.Objects;

/**
 * 启用标记枚举
 * @author zhouwq
 * @date 2019/10/23 15:59
 */
@Getter
@ToString
public enum EnableEnum {

    /**
     * 启用标记枚举
     */
    OPEN(1, "enum.enable.memo.open"),

    CLOSE(0, "enum.enable.memo.close"),

    UNKNOWN(404, "constant.i18nString.unknow")
    ;

    private Integer code;

    private String memo;

    EnableEnum(Integer code, String memo){
        this.code = code;
        this.memo = memo;
    }

    public String getMemo() {
        return I18nMessageUtil.getMessage(memo);
    }

    public static EnableEnum getByCode(Integer code) {
        for (EnableEnum cacheCode : values()) {
            if (Objects.equals(cacheCode.getCode(), code)) {
                return cacheCode;
            }
        }
        return UNKNOWN;
    }

    /**
     * enable int 转 boolean
     * @param enable
     * @return java.lang.Boolean
     * @author xinpeng.jiang
     * @date 2021/4/27 16:40
     * @version 1.0
     */
    public static Boolean transformEnable2Bool(Integer enable) {
        if (Objects.equals(enable, OPEN.getCode())) {
            return true;
        } else if (Objects.equals(enable, CLOSE.getCode())) {
            return false;
        }
        return null;
    }
}
